package com.amit.servlet;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class RegisterAction extends JLCBaseAction {
	
	Logger log=Logger.getLogger(RegisterAction.class);

	@Override
	public String service(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		log.info("RegisterAction-servlet");
		String result="register.jsp";
		
		String fn=request.getParameter("fname");
		String em=request.getParameter("email");
	    String ph=request.getParameter("phone");
	    String un=request.getParameter("username");
	    String pw=request.getParameter("password");
	    
	    request.setAttribute("FNM", fn);
	    request.setAttribute("EML", em);
	    request.setAttribute("PHN", ph);
	    request.setAttribute("UNM", un);
	   
	    boolean error=false;
	    long ph1=0;
	    if(JLCValidator.validateRequired(un))
	    {
	    	request.setAttribute("UNMERR", "Username is Required");
	    	error=true;
	    }
	    else if(JLCValidator.validateMaxLength(un,15))
	    {
	    	request.setAttribute("UNMERR", "Username must be maximum 15 character");
	    	error=true;
	    }
	    else if(JLCValidator.validateMinLength(un, 8))
	    {
	    request.setAttribute("UNMERR", "Username  must be minimum 8 character");
	    error=true;
	    }
	    
	    if(JLCValidator.validateRequired(pw))
	    {
	    	request.setAttribute("PWDERR", "Password is Required");
	    	error=true;
	    }
	    else if(JLCValidator.validateMaxLength(pw,15))
	    {
	    	request.setAttribute("PWDERR", "Password must be maximum 15 character");
	    	error=true;
	    }
	    else if(JLCValidator.validateMinLength(pw, 8))
	    {
	    request.setAttribute("PWDERR", "Password must be minimum 8 character");
	    error=true;
	    
	    }
	    
	    if(JLCValidator.validateRequired(fn))
	    {
	    	request.setAttribute("FNMERR", "Firstname is Required");
	    	error=true;
	    }
	    else if(JLCValidator.validateMaxLength(fn,35))
	    {
	    	request.setAttribute("FNMERR", "Firstname must be maximum 35 character");
	    	error=true;
	    }
	    else if(JLCValidator.validateMinLength(fn, 2))
	    {
	    request.setAttribute("FNMERR", "Firstname must be minimum 2 character");
	    error=true;
	    
	    }
	    
	    if(JLCValidator.validateRequired(ph))
	    {
	    	request.setAttribute("PHNERR", "Phone is Required");
	    	error=true;
	    }
	    else
	    {
	    	try
	    	{
	    		ph1=Long.parseLong(ph);
	    		if(JLCValidator.validateLength(ph, 10))
	    		{
	    			request.setAttribute("PHNERR", "Phone must be 10 Digits");
	    			error=true;
	    		}
	    	}
	    	catch (Exception e) {
	    		
	    		request.setAttribute("PHNERR", "Phone must be Digits");
	    		log.error("Phone is not number\n",e);
	    		error=true;
				
			}
	    }
	    if(JLCValidator.validateRequired(em))
	    {
	    	request.setAttribute("EMLERR", "Email is Required");
	    	error=true;
	    }
	    else if(JLCValidator.validateEmail(em))
	    {
	    request.setAttribute("EMLERR", "Provide Valid Email");
	    return result;
	    
	    }
	    
	    else if(JLCValidator.validateMaxLength(em,50))
	    {
	    	request.setAttribute("EMLERR", "Email must be maximum 50 character");
	    	error=true;
	    }
	    else if(JLCValidator.validateMinLength(em, 9))
	    {
	    request.setAttribute("EMLERR", "Email must be minimum 9 character");
	    error=true;
	    
	    }
	    
	    if(error)
	    {
	    	return result;
	    }
	    
	    UserTo uto=new UserTo(0,fn,em,ph1,un,pw);
	    int x=userService.registerUser(uto);
	    if(x==1)
	    {
	    	HttpSession session=request.getSession();
	    	session.setAttribute("UN", un);
	    	result="home.jsp";
	    	String sub="Registration Success";
	    	String body="<h1>Hi "+fn+"Your Registarion has been complted successfullly</h1>";
	    	
	    	MailService.sendMail(em,sub,body);
	   
	    }
	    else
	    {
	    	request.setAttribute("MSG", "Registarion Failed-Try Again");
	    }

		return result;
	}
}
